package com.qms.repast.module;

import android.app.Activity;

import com.qms.repast.entity.reqbean.ReqDishesInfo;
import com.qms.repast.entity.reqbean.ReqFood;
import com.qms.repast.entity.reqbean.ReqRemark;
import com.qms.repast.entity.reqbean.SearchBean;
import com.qms.repast.entity.reqbean.ShopId;
import com.qms.repast.entity.resbean.BannerBean;
import com.qms.repast.entity.resbean.CityBean;
import com.qms.repast.entity.resbean.DishesInfo;
import com.qms.repast.entity.resbean.FoodBean;
import com.qms.repast.entity.resbean.RemarkBean;
import com.qms.repast.entity.resbean.ShopDetailInfo;
import com.qms.repast.entity.resbean.ShopInfo;
import com.qms.repast.entity.resbean.UserInfo;
import com.qms.repast.net.ServiceFactory;
import com.qms.repast.net.cache.CacheFactory;
import com.qms.repast.net.function.RetryFunction;
import com.qms.repast.service.IUserService;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.rx_cache2.EvictProvider;

/**
 * @author 泅渡者
 * @date 2018/6/6 0006.
 * @describe: 操作与用户相关的接口
 */

public class UserModule {

	public static final IUserService userService = ServiceFactory.getUserServiceInstance();
	private RxAppCompatActivity rxAppCompatActivity;

	public UserModule (Activity context) {
		rxAppCompatActivity = (RxAppCompatActivity) context;
	}

	/**
	 * 用户登录
	 *
	 * @param observer
	 */
	public void reqLogin (Map<String, String> params, Observer observer) {
		Observable<UserInfo> observable = userService.getLogin(params);
		observable.subscribeOn(Schedulers.io())
				.unsubscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.retryWhen(new RetryFunction())
				.compose(rxAppCompatActivity.bindUntilEvent(ActivityEvent.DESTROY))
				.subscribe(observer);
	}

	/**
	 * 获取城市列表
	 *
	 * @param observer
	 */
	public void reqCityList (Observer observer) {
		Observable<CityBean> observable = userService.getCityList();
		CacheFactory.getCacheProviders()
				.getCacheCityList(observable, new EvictProvider(false))
				.subscribeOn(Schedulers.io())
				.retryWhen(new RetryFunction())
				.unsubscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.compose(rxAppCompatActivity.bindUntilEvent(ActivityEvent.DESTROY))
				.subscribe(observer);
	}

	/**
	 * 获取首页轮播图
	 *
	 * @param observer
	 */
	public void reqBannerList (Observer observer) {
		Observable<BannerBean> observable = userService.getBannerList();
		CacheFactory.getCacheProviders()
				.getCacheBannerList(observable, new EvictProvider(false))
				.subscribeOn(Schedulers.io())
				.retryWhen(new RetryFunction())
				.unsubscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.compose(rxAppCompatActivity.bindUntilEvent(ActivityEvent.DESTROY))
				.subscribe(observer);
	}

	/**
	 * 获取商家列表
	 *
	 * @param observer
	 */
	public void reqShopList (SearchBean searchBean, Observer observer) {
		Observable<ShopInfo> observable = userService.getShopList(searchBean);
		observable.subscribeOn(Schedulers.io())
				.unsubscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.retryWhen(new RetryFunction())
				.compose(rxAppCompatActivity.bindUntilEvent(ActivityEvent.DESTROY))
				.subscribe(observer);
	}

	/**
	 * 商铺基本信息
	 *
	 * @param observer
	 */
	public void reqShopInfo (ShopId shopId, Observer observer) {
		Observable<ShopDetailInfo> observable = userService.getShopInfo(shopId);
		observable.subscribeOn(Schedulers.io())
				.unsubscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.retryWhen(new RetryFunction())
				.compose(rxAppCompatActivity.bindUntilEvent(ActivityEvent.DESTROY))
				.subscribe(observer);
	}

	/**
	 * 获取菜品列表
	 *
	 * @param observer
	 */
	public void reqFoodList (ReqFood reqFood, Observer observer) {
		Observable<FoodBean> observable = userService.getFoodList(reqFood);
		observable.subscribeOn(Schedulers.io())
				.unsubscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.retryWhen(new RetryFunction())
				.compose(rxAppCompatActivity.bindUntilEvent(ActivityEvent.DESTROY))
				.subscribe(observer);
	}

	/**
	 * 获取菜品列表
	 *
	 * @param observer
	 */
	public void reqDishInfo (ReqDishesInfo reqDishesInfo, Observer observer) {
		Observable<DishesInfo> observable = userService.getDishInfo(reqDishesInfo);
		observable.subscribeOn(Schedulers.io())
				.unsubscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.retryWhen(new RetryFunction())
				.compose(rxAppCompatActivity.bindUntilEvent(ActivityEvent.DESTROY))
				.subscribe(observer);
	}

	/**
	 * 获取订单备注数据
	 *
	 * @param observer
	 */
	public void reqRemark (ReqRemark reqRemark, Observer observer) {
		Observable<RemarkBean> observable = userService.getRemarks(reqRemark);
		observable.subscribeOn(Schedulers.io())
				.unsubscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.retryWhen(new RetryFunction())
				.compose(rxAppCompatActivity.bindUntilEvent(ActivityEvent.DESTROY))
				.subscribe(observer);
	}
}
